//
//  QuickMesTableViewController.m
//  VEW
//
//  Created by lanou3g on 15/7/18.
//  Copyright (c) 2015年 Y团. All rights reserved.
//

#import "QuickMesTableViewController.h"
#import "MJRefresh.h"
#import "NomalModel.h"
#import "NomalImageTableViewCell.h"
#import "NoImageTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "AFHTTPRequestOperationManager.h"
#define NoImageCell @"cell1"
#import "DetailViewController.h"
#import "HomeModel.h"


@interface QuickMesTableViewController ()

@property (nonatomic ,strong) NSMutableArray * dataArray;

@property (nonatomic ,assign) NSInteger  pageNum;

@property (nonatomic ,assign) BOOL isRefresh;

@property (nonatomic ,strong) NSString *myurl;

@end

@implementation QuickMesTableViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    
    [self.tableView registerClass:[NomalImageTableViewCell class] forCellReuseIdentifier:NoImageCell];
    
    self.dataArray = [NSMutableArray array];

    self.myurl = @"http://api.m.jiemian.com/article/cate/141.json";
    
    self.pageNum = 1;
    
    [self data];
    [self setupRefresh];
   

    
    
}




-(void)setupRefresh
{
    //下拉刷新
    [self.tableView addHeaderWithTarget:self action:@selector(headerRereshing)];
    
    [self.tableView headerBeginRefreshing];
    
    
    //上拉加载跟多(进入刷新状态就会调用self的footerRefreshing)
    
    [self.tableView addFooterWithTarget:self action:@selector(footerRereshing)];
    
    self.tableView.headerPullToRefreshText = @"下拉可以刷新了";
    
    self.tableView.headerReleaseToRefreshText = @"松开马上刷新了";
    
    self.tableView.headerRefreshingText = @"刷新中。。。";
    
    self.tableView.footerPullToRefreshText = @"上拉可以加载更多数据了";
    
    self.tableView.footerReleaseToRefreshText = @"松开马上加载更多数据了";
    
    self.tableView.footerRefreshingText = @"加载中。。。";
    
    
    
}


//下拉刷新
- (void)headerRereshing
{
    _pageNum = 1;
    self.isRefresh = NO;
    [self data];
    [self.tableView headerEndRefreshing];

}
     
     
// 上拉加载
-(void)footerRereshing
{
    _pageNum ++;
    self.isRefresh = YES;
    [self data];
    [self.tableView footerEndRefreshing];
    
   
}

-(void)data{
    // 网络请求
    AFHTTPRequestOperationManager * manager= [AFHTTPRequestOperationManager manager];
    NSString *url = [self.myurl stringByAppendingString:[NSString stringWithFormat:@"?&page=%ld",self.pageNum]];
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (!self.isRefresh) {
            [self.dataArray removeAllObjects];
        }
        NSDictionary *dic = responseObject[@"result"];
        NSArray *array1 =dic[@"rst"];
        for (NSDictionary *dic1 in array1) {
            NomalModel * normal = [NomalModel new];
            normal.title = dic1[@"title"];
            normal.MyID = dic1[@"id"];
            normal.publishTime = dic1[@"publishtime"];
            normal.comment = dic1[@"comment"];
            [self.dataArray addObject:normal];
        }
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
    [self.tableView reloadData];

    }

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    // Return the number of rows in the section.
    return self.dataArray.count;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      NomalModel * normal =self.dataArray[indexPath.row];
        NoImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NoImageCell forIndexPath:indexPath];
        // 给cell赋值
        cell.commentLabel.text = [NSString stringWithFormat:@"评论(%@)", normal.comment];
    cell.commentLabel.font=[UIFont fontWithName:@"TimesNewRomanPSMT" size:15];
        cell.titleLabel.text = normal.title;
        cell.timeLabel.text =  [self timeFormatted:[normal.publishTime integerValue]];
  
    //发布label自适应
    CGSize size2 = [cell.timeLabel sizeThatFits:CGSizeMake(cell.timeLabel.frame.size.width, MAXFLOAT)];
    cell.timeLabel.frame = CGRectMake(cell.timeLabel.frame.origin.x, cell.timeLabel.frame.origin.y, size2.width, size2.height);
    
    //布局
        cell.titleLabel.frame = CGRectMake(10, 10, cell.frame.size.width-20, cell.titleLabel.frame.size.height);
         cell.timeLabel.frame =CGRectMake(10, cell.titleLabel.frame.size.height+20    , cell.timeLabel.frame.size.width, cell.timeLabel.frame.size.height);
        cell.commentLabel.frame = CGRectMake(CGRectGetWidth([[UIScreen mainScreen]bounds])-60, cell.timeLabel.frame.origin.y, 60, cell.timeLabel.frame.size.height);
    
        return cell;
    }



    
    
    // 将秒数转化成日期
- (NSString *)timeFormatted:(long long)totalSeconds
    {
        NSDate *date = [NSDate dateWithTimeIntervalSince1970:totalSeconds];
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"MM/dd HH:mm:ss"];
        NSString *showtimeNew = [formatter stringFromDate:date];

        return showtimeNew;
    }

    
     
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NomalModel *normal= _dataArray[indexPath.row];
    return 90;
}

    
    
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 刚选中又马上取消选中，格子不变色
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    UIViewController *vc = [[UIViewController alloc]init];
    vc.view.backgroundColor = [UIColor yellowColor];
    
    DetailViewController *detailVC = [[DetailViewController alloc]init];
    
    HomeModel *home = [[HomeModel alloc]init];
    
    home = self.dataArray[indexPath.row];
    
    detailVC.homeData = home;
    
    detailVC.hidesBottomBarWhenPushed=YES;
    
    [self.navigationController pushViewController:detailVC animated:YES];
}





@end
